<?php

namespace app\common\command;

use app\admin\model\Membercoupon;
use app\admin\model\MessageNotice;
use app\api\service\Message as MessageService;

use think\Exception;

class Coupon
{

    /**
     * 优惠券到期提醒
     */
    public static function cron(){
        try {
            $model = new \app\admin\model\Member();

            $where = [
                'm_status' => 1
            ];
            $field = 'id, c_id';
            // 所有用户
            $memberIds = $model->where($where)->field($field)->select();
            if($memberIds){
                foreach ($memberIds as $item) {
                    $memberId = $item['id'];
                    // 查看是否有快过期的优惠券 -- 提前7天
                    $beginTime = date('Y-m-d 00:00:00', strtotime('-7 days'));
                    $endTime = date('Y-m-d 23:59:59', strtotime('-7 days'));
                    $membercoupons = Membercoupon::alias('a')->where(['a.member_id ' => $memberId, 'a.status' => 1, 'c.end_time' => ['between time', [$beginTime, $endTime]]])->join('__COUPON__ c','a.coupon_id = c.id')->field('c.id, c.end_time')->select();
                    if ($membercoupons) {
                        foreach ($membercoupons as $membercoupon) {
                            // 添加账户通知消息 -- 优惠券过期提醒消息
                            $couponMesData = [
                                'create_date' => date('Y-m-d H:i:s'),
                                'member_id' => $memberId,
                                'img_url' => '',
                                'title' => '优惠券到期提醒',
                                'detail' => '您有1张优惠券即将到期，请尽快使用',
                                'type' => 1,
                                'related_id' => $membercoupon['id']
                            ];
                            $couponMesInfo = MessageNotice::where(['member_id' => $memberId, 'type' => 1, 'related_id' => $membercoupon['id']])->field('id')->find();
                            if ($couponMesInfo) {
                                continue;
                            }
                            $couponMesRes = MessageNotice::insert($couponMesData);

                            if ($couponMesRes !== false) {
                                trace($item['id'] . ': 优惠券到期提醒成功','log');
                            } else {
                                trace($item['id'] . ': 优惠券到期提醒失败','notice');
                            }

                            // 推送送APP消息
                            $mesData = [
                                'template_type' => 1,
                                'template_data' => [
                                    'transmission_type' => 1,
                                    'transmission_content' => '',
                                    'title' => '优惠券到期提醒',
                                    'text' => '您有1张优惠券即将到期，请尽快使用',
                                    'logo'  => '',
                                    'logo_url'  => '',
                                ],
                                'cid' => $item['c_id'], // 推送通知至指定用户时填写
                                'cid_list' => '', // 推送通知至指定用户列表时填写
                            ];
                            MessageService::pushNotice($mesData);
                        }
                    }
                }
            }
        } catch (Exception $e) {
            trace($e->getMessage(),'error');
        }
    }
}